package com.button.boot.mail.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.button.boot.mail.model.MailModel;
import com.button.boot.mail.service.MailService;

@RestController
public class MailController {
	private static final Logger LOGGER = LoggerFactory.getLogger(MailController.class);
	private static final String MAIL_FROM = "182*****@163.com";
	private static final String MAIL_TO = "274*****@qq.com";
	private static final String MAIL_TO2 = "252*****@qq.com";
	@Autowired
	private MailService mailService;

	@GetMapping(value = "/sendSimpleMail")
	public void sendSimpleMail() {
		try {
			MailModel mailModel = new MailModel();
			mailModel.setFrom(MAIL_FROM);
			mailModel.setTo(MAIL_TO);
			mailModel.setSubject("SendSimpleMail");
			mailModel.setContent("测试发送普通邮件");
			mailService.sendSimpleMail(mailModel);
		} catch (Exception e) {
			LOGGER.error("sendSimpleMail邮件发送出现异常. ex={}", e);
		}
	}
	
	@GetMapping(value = "/sendHTMLMail")
	public void sendHTMLMail() {
		try {
			MailModel mailModel = new MailModel();
			mailModel.setFrom(MAIL_FROM);
			mailModel.setTo(MAIL_TO);
			mailModel.setSubject("sendHTMLMail");
			StringBuilder sb = new StringBuilder();
            sb.append("<h1>SpirngBoot测试邮件发送(HTML格式)</h1>")
                    .append("<p style='color:#F00'>这里是发送邮件的文本内容！</p>");
			mailModel.setContent(sb.toString());
			mailService.sendHTMLMail(mailModel);
		} catch (Exception e) {
			LOGGER.error("sendHTMLMail邮件发送出现异常. ex={}", e);
		}
	}
	
	@GetMapping(value = "/sendAttachmentMail")
	public void sendAttachmentMail() {
		try {
			MailModel mailModel = new MailModel();
			mailModel.setFrom(MAIL_FROM);
			mailModel.setTo(MAIL_TO);
			mailModel.setSubject("sendAttachmentMail");
			mailModel.setContent("这是一封带有附件的邮件!!");
			mailModel.setResourcePath("pusa.png");
			mailService.sendAttachmentMail(mailModel);
		} catch (Exception e) {
			LOGGER.error("sendAttachmentMail邮件发送出现异常. ex={}", e);
		}
	}
	
	@GetMapping(value = "/sendInlineResourceMail")
	public void sendInlineResourceMail() {
		try {
			MailModel mailModel = new MailModel();
			mailModel.setFrom(MAIL_FROM);
			String[] mailTo = {MAIL_TO, MAIL_TO2};
			mailModel.setTo(mailTo);
			mailModel.setSubject("sendInlineResourceMail");
			mailModel.setContent("这是一封镶嵌静态资源的邮件!!");
			mailModel.setResourcePath("src/main/resources/templates/resource.html");
			mailModel.setResourceId("静态资源");
			mailService.sendInlineResourceMail(mailModel);
		} catch (Exception e) {
			LOGGER.error("sendAttachmentMail邮件发送出现异常. ex={}", e);
		}
	}
}
